﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Easily
{
    /// <summary>
    /// 说明：本窗体为SPI类传感器配置窗体
    /// </summary>
    public partial class frmTESV : Form
    {
        private GeneralIcon MyIcon = null;  //保存传入的控件对象
        /// <summary>
        ///构造函数
        ///主要功能：加载保存的控件设置；生成初始代码；
        public frmTESV(GeneralIcon icon)
        {
            InitializeComponent();
            this.MyIcon = icon;
            this.Text = this.MyIcon.ModuleName;    //根据控件名改变窗体文本
            //添加头文件
            this.MyIcon.code.HeadFile = "#include \"tsev.h\"";
            //添加spi模块号
            this.c_Box_ModuleSelect.Items.Add("SPI_0");
            this.c_Box_ModuleSelect.Items.Add("SPI_1");
            //添加像素位选择
            this.c_Box_PixelSelect.Items.Add("0xA0");    //传感器温度
            this.c_Box_PixelSelect.Items.Add("0xA1");    //对应8像素位温度
            this.c_Box_PixelSelect.Items.Add("0xA2");    //对应8像素位温度
            this.c_Box_PixelSelect.Items.Add("0xA3");    //对应8像素位温度
            this.c_Box_PixelSelect.Items.Add("0xA4");    //对应8像素位温度
            this.c_Box_PixelSelect.Items.Add("0xA5");    //对应8像素位温度
            this.c_Box_PixelSelect.Items.Add("0xA6");    //对应8像素位温度
            this.c_Box_PixelSelect.Items.Add("0xA7");    //对应8像素位温度
            this.c_Box_PixelSelect.Items.Add("0xA8");    //对应8像素位温度
            //默认选择
            c_Box_ModuleSelect.SelectedIndex = 1;
            c_Box_PixelSelect.SelectedIndex = 0;
            //恢复上次设置
            if (this.MyIcon.code.Save != "")
            {
                //保存的内容包括：（1）spi模块号；（2）选择的像素
                string[] tmp = this.MyIcon.code.Save.Split(';');
                if (tmp.Length == 2)
                {
                    this.c_Box_ModuleSelect.SelectedItem = tmp[0];
                    this.c_Box_PixelSelect.SelectedItem = tmp[1];
                }
            }
            btn_confirm_Click(new object(), new EventArgs());

        }

        private void btn_confirm_Click(object sender, EventArgs e)
        {
            MyIcon.code.Save = "";
            //初始化代码
            MyIcon.code.Initialization = "tsev_init(" + c_Box_ModuleSelect.SelectedItem.ToString() + ");";
            MyIcon.code.Save += c_Box_ModuleSelect.SelectedItem.ToString() + ";";

            if (MyIcon.code.GlobalVariable.Contains("bTSEV") == false)
            {
                MyIcon.code.GlobalVariable += "uint16_t bTSEV";
            }
            MyIcon.code.InsertCode = "bTSEV=tsev_read(" + c_Box_PixelSelect.SelectedItem.ToString() + ");\n    //读取传感器返回值";
            MyIcon.code.Save += c_Box_PixelSelect.SelectedItem.ToString();
            MyIcon.code.Comment = "读取" + this.Text + "的值";
            this.DialogResult = DialogResult.OK;
        }


        /// <summary>
        /// 单击“取消”按钮
        /// 主要功能：设置返回结果为取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }


        /// <summary>
        /// 点击“注释说明”按钮
        /// 功能概要：为控件添加自定义注释说明
        /// </summary>
        private void btn_comment_Click(object sender, EventArgs e)
        {
            using (frmNodeEditer NE = new frmNodeEditer(this.MyIcon.code.Comment))
            {
                if (NE.ShowDialog() == DialogResult.OK)
                    this.MyIcon.code.Comment = NE.NoteEdit;
            }
        }
    }
}
